/*----------------------------------------------
| noka switch v1.0 www.97521.com               |
| rebin 2017-04-20                             |
|---------------------------------------------*/
var nswitch = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.id = cfg.id;				//id
    	this.value = (undefined==cfg.value?0:cfg.value);			//值
    	this.name = cfg.name;			//名字
    	this.ope = (undefined==cfg.open?{title:'YES',value:'1'}:cfg.open);
    	this.off = (undefined==cfg.off?{title:'OFF',value:'0'}:cfg.off);
    	this.onclick = cfg.onclick; 	//单击
    	this.readonly = cfg.readonly;
    	this.disabled = cfg.disabled;
    	this.onchange = cfg.onchange;
    	this.width = cfg.width;
    	this.cfg = cfg;
    },
    show : function(){
    	var self = this;
    	self.initStar();
    	self.RowEvent();
    	self.starevent();
    },
    //-------------------初始化-----------------------------
    initStar : function(){
    	var self = this;
    	var idx = 0;
    	var html = [];
    	if(undefined==self.width){
    		self.width = 60;
    	}
    	if(undefined==self.value){
    		self.value = self.off.value;
    	}
    	html[idx++]='<input type="hidden" id="'+self.id+'" name="'+(undefined==self.name?'':self.name)+'" value="'+self.value+'"/>';
    	if(self.value == self.ope.value){//打开状态
    		html[idx++]='<input type="hidden" id="'+self.id+'_sinput" name="'+(undefined==self.name?'':self.name)+'_sinput" value="'+self.ope.title+'"/>';
    		html[idx++]='<div id="nk_'+self.id+'_switch_open" class="noka_switch_open" style="width:'+self.width+'px">'+self.ope.title+'</div>';
        	html[idx++]='<div id="nk_'+self.id+'_switch_close" class="noka_switch_back" style="width:'+self.width+'px">&nbsp;</div>';
    	}else{
    		html[idx++]='<input type="hidden" id="'+self.id+'_sinput" name="'+(undefined==self.name?'':self.name)+'_sinput" value="'+self.off.title+'"/>';
    		html[idx++]='<div id="nk_'+self.id+'_switch_open" class="noka_switch_back" style="width:'+self.width+'px">&nbsp;</div>';
        	html[idx++]='<div id="nk_'+self.id+'_switch_close" class="noka_switch_close" style="width:'+self.width+'px">'+self.off.title+'</div>';
    	}
    	$(self.id+'_div').innerHTML = html.join('');
    	self.setDisabled('disabled'==self.disabled);
    },
    //----------------选项事件-----------------------------
    RowEvent : function(){
    	var self = this;
    	$(self.id+'_div').observe('click', function() {
    		if('readonly'==self.readonly || 'disabled'==self.disabled){
    			return;
    		}
    		if(self.ope.value==self.value){
        		self.value = self.off.value;
    			$(self.id).value = self.value;
    		}else{
    			self.value = self.ope.value;
    			$(self.id).value = self.value;
    		}
    		self.setChecked();
    	});
    },
    //---------------选择------------------------------------
    setChecked : function(){
    	var self  = this;
    	if(self.off.value==self.value){
    		$('nk_'+self.id+'_switch_open').removeClassName('noka_switch_open');
			$('nk_'+self.id+'_switch_open').addClassName('noka_switch_back');
			$('nk_'+self.id+'_switch_close').removeClassName('noka_switch_back');
			$('nk_'+self.id+'_switch_close').addClassName('noka_switch_close');
			$('nk_'+self.id+'_switch_open').innerHTML='&nbsp;';
			$('nk_'+self.id+'_switch_close').innerHTML=self.off.title;
			$(self.id+'_sinput').value = self.off.title;
    	}else{
    		$('nk_'+self.id+'_switch_open').removeClassName('noka_switch_back');
			$('nk_'+self.id+'_switch_open').addClassName('noka_switch_open');
			$('nk_'+self.id+'_switch_close').removeClassName('noka_switch_close');
			$('nk_'+self.id+'_switch_close').addClassName('noka_switch_back');
			$('nk_'+self.id+'_switch_open').innerHTML=self.ope.title;
			$('nk_'+self.id+'_switch_close').innerHTML='&nbsp;';
			$(self.id+'_sinput').value = self.ope.title;
    	}
    	if(undefined != self.onclick){
	        try{ self.onclick($(self.id)); }catch(e){}
	    }
	    if(undefined != self.onchange){
	        try{ self.onchange($(self.id)); }catch(e){}
	    }
    },
    setDisabled : function(t){
    	var self = this;
    	self.disabled = (t?'disabled':'');
    	if('disabled'==t){
    		$('nk_'+self.id+'_switch_open').addClassName('noka_switch_disabled');
    		$('nk_'+self.id+'_switch_close').addClassName('noka_switch_disabled');
    	}else{
    		$('nk_'+self.id+'_switch_open').removeClassName('noka_switch_disabled');
    		$('nk_'+self.id+'_switch_close').removeClassName('noka_switch_disabled');
    	}
    },
    //---------------外部事件---------------------------------------------------------
    starevent : function(){
    	var self = this;
    	$(self.id).veri = function(){
    		return true;
    	};
    	//---------设置值-----------------------------------------------------------
    	$(self.id).setValue = function(value,ch){
    		$(self.id).value = value;
    		self.value = value;
    		self.setChecked();
    	};
    	//---------获取值--------------------------------------------------------
    	$(self.id).getValue = function(){
    		return $(self.id).value;
    	};
    	//---------设置只读-----------------------------------------------------
    	$(self.id).setReadonly = function(a){
    		self.readonly = (a?'readonly':'');
    	};
    	$(self.id).setDisabled = function(a){
    		self.setDisabled(a?'disabled':'');
    	};
    	//-----------动态表单--------------------------------------------------
    	document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
   		if(self.id.indexOf('_nkADinsert_')==-1){
	    		var npfffs=event.memo.widgetNumber.clone();//[{}]
	    		for(var i=0;i<npfffs.length;i++){
	    			var cfgid = Object.allclone(self.cfg);
	    			var initem = npfffs[i];
	    			cfgid.id = initem.id;
	    			if(cfgid.id.indexOf('_'+self.id+'_')!=-1){
	    				(new nswitch(cfgid)).show();
	    				try{
	    					var idx = cfgid.id.substring(cfgid.id.lastIndexOf('_')+1,cfgid.id.length);
	    					$(cfgid.id).name=$(cfgid.id).name.replace(/\[0\]/g,'['+idx+']');
	    					if(undefined!=initem.value && $(cfgid.id).name==initem.name){
	    						$(cfgid.id).setValue(initem.value);
	    					}
	    				}catch(e){}
	    			}
	    		}
   		 }
   	 });
    }
});